package cn.smbms.controller;

import java.util.Date;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.pojo.Bill;
import cn.pojo.User;
import cn.smbms.service.BillService;
import cn.smbms.service.ProviderService;
import cn.smbms.tools.Constants;
import cn.smbms.tools.Page;

@Controller
@RequestMapping("bill")
public class BillController {
	@Resource
	private BillService bsi;
	@Resource
	private ProviderService psi;
	
	@RequestMapping("list.html")
	public String billList(@RequestParam(value="queryProductName",required=false)String name,
						@RequestParam(value="queryProviderId" ,required=false)String proId,
						@RequestParam(value="queryIsPayment",required=false)String isPayment,
						@RequestParam(value="pageIndex" ,required=false)String index,
						Model model){
		if(index==null){
			index="1";
		}
		if(proId!=null&&proId.equals("0")){
			proId=null;
		}
		if(isPayment!=null&&isPayment.equals("0")){
			isPayment=null;
		}
		
		Page page=new Page();
		int _index=Integer.parseInt(index);
		page.setCurPage(_index);
		page.setTotalCount(bsi.getCount(name, proId, isPayment));
		model.addAttribute(page);
		model.addAttribute("billList", bsi.getBillList(name, proId, isPayment,_index));
		model.addAttribute("providerList",psi.getList());
		model.addAttribute("queryProductName",name);
		model.addAttribute("queryProviderId",proId);
		model.addAttribute("queryIsPayment",isPayment);
		return "billlist";
	}
	
	@RequestMapping("toAdd")
	public String toAdd(){
		return "billadd";
	}
	
	@RequestMapping("providerList")
	@ResponseBody
	public Object getProviderList(){
		return psi.getList();
	}
	
	@RequestMapping("saveAdd")
	public String saveAdd(Bill bill,HttpSession session){
		User user=(User)session.getAttribute(Constants.USER_SESSION);
		bill.setCreatedBy(user.getId());
		bill.setCreationDate(new Date());
		if(bsi.addBill(bill)>0){
			return "redirect:/bill/list.html";
		}else{
			return "redirect:/bill/toAdd";
		}
	}
	
	@RequestMapping("toUp")
	public String toUp(String id,Model model){
		model.addAttribute("bill", bsi.getBillById(id));
		return "billmodify";
	}
	
	@RequestMapping("saveUp")
	public String saveUp(Bill bill,HttpSession session){
		if(bsi.upBill(bill)>0){
			return "redirect:/bill/list.html";
		}else{
			return "billmodify";
		}
	}
	
	@RequestMapping("toView")
	public String toView(String id,Model model){
		model.addAttribute("bill",bsi.getBillById(id));
		return "billview";
	}
	
	@RequestMapping("del")
	@ResponseBody
	public Object del(String id){
		HashMap<String,String> resultMap=new HashMap<String,String>();
		String result="notexist";
		int num=bsi.delBill(id);
		if(num>0){
			result="true";
		}else if(num<0){
			result="false";
		}
		resultMap.put("delResult", result);
		return resultMap;
	}
}
